package com.cg.controller;

import com.cg.client.SystemClient;
import com.cg.dto.Result;
import com.cg.dto.UpdateUserDto;
import com.cg.dto.UserDepRoleDto;
import com.cg.dto.UserRoleDto;
import com.cg.entity.User;
import com.cg.utils.TokenUtil;
import com.cg.vo.LoginUserVo;
import com.cg.vo.UserDetailVo;
import com.cg.vo.UserShowInfoVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author CG
 * @Date 2023/12/1 11:54
 **/
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private SystemClient systemClient;

    /**
     * 用UUID生成codeNum
     */
    @PostMapping("/createCodeNum")
    public Result<String> createCodeNum(){
        return systemClient.createCodeNum();
    }


    /**
     * 添加新用户
     * @param user:前端传过来要添加的用户信息
     */
    @PostMapping("/insertUser")
    public Result createUser(HttpServletRequest request,User user){
        String token = request.getHeader("token");
        //获取UserId
        Integer userId = TokenUtil.getUserIdByToken(token);
        //将userId放进对象中
        user.setCreateUser(userId);
        return systemClient.createUser(user);
    }


    /**
     * 用户登录
     * @param user：前端传递过来的用户登录名和密码
     * @return：返回的数据
     */
    @PostMapping("/login")
    public Result<LoginUserVo> login(User user){
        return systemClient.login(user);
    }


    /**
     * 通过Token获取用户信息
     * @return：用户信息
     */
    @PostMapping("/getUserByToken")
    public LoginUserVo getUserByToken(HttpServletRequest request){
        //获取token
        String token = request.getHeader("token");
        return systemClient.getUserByToken(token);

    }

    /**
     * 员工管理页面分页并按照名称模糊查询
     */
    @PostMapping("/list")
    public Result<Map<String,Object>> queryUserByUserName(UserDepRoleDto userDepRoleDto){
        return systemClient.queryUserByUserName(userDepRoleDto);
    }


    /**
     * 用户授权
     * @param userRoleDto
     * @return
     */
    @PostMapping("/addUserForRole")
    public Result createUserRole(@RequestBody UserRoleDto userRoleDto){
        return systemClient.createUserRole(userRoleDto);
    }


    /**
     * 用户详情展示
     */
    @GetMapping("/getUserByUserId")
    public Result<UserDetailVo> getUserByUserId(Integer userId){
        return systemClient.getUserByUserId(userId);
    }


    /**
     * 启用/禁用
     */
    @PostMapping("/updateUserState")
    public Result updateUserState(Integer id){
        return systemClient.updateUserState(id);
    }

    /**
     * 修改员工信息
     * @param user
     * @return
     */
    @PostMapping("/updateUserByUserId")
    public Result updateUserByUserId(User user){
        return systemClient.updateUserByUserId(user);
    }

    /**
     * 删除员工
     */
    @PostMapping("/removeUserByUserId")
    public Result removeUserByUserId(Integer userId){
        return systemClient.removeUserByUserId(userId);
    }


    /**
     * 根据用户id查询个人的详细信息
     */
    @GetMapping("/queryUserInfo")
    public Result<UserShowInfoVo> queryUserInfo(Integer userId){
        return systemClient.queryUserInfo(userId);
    }


    /**
     * 验证登录名是否可用
     */
    @PostMapping("/checkLoginUser")
    public Result checkLoginUser(String loginUser){
        return systemClient.checkLoginUser(loginUser);
    }

    /**
     * 修改密码
     */
    @PostMapping("/setPassword")
    public Result setPassword(UpdateUserDto updateUserDto){
        return systemClient.setPassword(updateUserDto);
    }


    /**
     * 修改用户登录名
     */
    @PostMapping("/setLoginUser")
    public Result setLoginUser(User user){
        return systemClient.setLoginUser(user);
    }
}
